import ResUtil from "@/utils/resUtil";
import AxiosManager from "../axiosManager";
import {Case, CaseStatus} from "@/types/response/case";
import Repayment from "@/types/response/repayment";
import PaymentHistory from "@/types/response/paymentHistory";
import MediatorComment from "@/types/response/mediatorComment";
let instance = AxiosManager.caseAxios;
type CaseStatistic={
    totalNum:number,
    unDoNum:number,
    finishNum:number,
    successNum:number,
    finishedRate:number,
    successRate:number
}
class CaseStatisticLuo{
    static toArray(c:CaseStatisticLuo){
        let data:{name:string,value:number}[]=[];
        for(let key in c){
          let val={name:key,value:c[key]}
          data.push(val)
        }
        return data;
    }
    待调解:number;
    待助理联系:number;
    调解成功:number;
    调解失败:number;
    废弃:number
}
async function getCaseStatisticLuo(){
    let res=await instance.get("/case/statistics/luo");
    return ResUtil.getData<CaseStatisticLuo>(res);
}
//
async function selectSelective(c:Case,beginMRangeTime?:String,endMRangeTime?:String){
    let res=await instance.post("/case/selectSelective",c,{params:{beginMRangeTime,endMRangeTime}});
    if(ResUtil.isSuccess(res)){
        return ResUtil.getData<Case[]>(res);
    }
}
//获得未完成的案件
async function getUnCompletedCases(){
    let res=await instance.get("/cases/unCompleted");
    return ResUtil.getData<Case[]>(res);
}
async function getCompletedCases() {
    let res=await instance.get("/cases/completed");
    return ResUtil.getData<Case[]>(res);
}
async function getRefusedCase(){
    return getCaseByCaseStatus(CaseStatus.inject);
}
//获得系统的数据统计
async function getCaseStatistic(){
   let res=await instance.get("/case/statistic");
   return ResUtil.getData<CaseStatistic>(res);
}
//获得与用户相关的案件
async function getCaseByUserId(userId:number){
    let res=await instance.get("/case/user",{params:{userId}});
    return ResUtil.getData<Case[]>(res);
}
//获得调解员未处理完的案件
async function getUnCompletedCaseByMediatorId(mediatorId:number){
    let res=await instance.get("/case/mediator/unCompleted",{
        params:{mediatorId}
    });
    return ResUtil.getData<Case[]>(res);
}
//获得调解员已完成的案件
async function getCompletedCaseByMediatorId(mediatorId:number){
    let res=await instance.get("/case/mediator/completed",{
        params:{mediatorId}
    });
    return ResUtil.getData<Case[]>(res);
}
//根据调解状态获得案件
async function getCaseByCaseStatus(caseStatus:number){
    let res=await instance.get("/case/caseStatus",{params:{caseStatus}});
    return ResUtil.getData<Case[]>(res);
}
//增加案件
async function addCase(c: Case) {
    let res = await instance.post("/case", c);
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Case>(res);
    }
    return null;
}
//申请人提交啊案件申请

async function applyCase(c: Case) {
    let res = await instance.post("/case/apply", c);
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Case>(res);
    }
    return null;
}
async function addCaseRepayment(caseId:number,repaymentIds:number[]) {
    if(repaymentIds==null||repaymentIds.length==0) return true;
    let res = await instance.post("/repaymentToCase", null,{params:{caseId,repaymentIds}})
    return ResUtil.isSuccess(res);
}
async function getCaseRepayment(caseId:number) {
    let res = await instance.get("/repaymentToCase",{params:{caseId}})
    if (ResUtil.isSuccess(res)){
        return ResUtil.getData<Repayment[]>(res);
    }
}
//申请人修改案件
async function updateByApplicant(c: Case) {
    let res = await instance.put("/case/applicant", c);
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Case>(res);
    }
    return null;
}
//获得全部案件
async function getAllCase() {
    let res = await instance.get("/cases");
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Case[]>(res);
    }
    return null;
}
//助理设置案件的被申请人
async function setRespondent(caseId:number,rspId:number){
   let res=await instance.put("/case/setRespondent",null,{
        params:{caseId,rspId}
    })    
    return ResUtil.isSuccess(res);
}
//根据案件id获得案件详细信息
async function getCaseById(caseId: number) {
    let res = await instance.get("/case", {
        params: { caseId }
    });
    if (ResUtil.isSuccess(res)) {
        return ResUtil.getData<Case>(res);
    }
    return null;
}
//申请人发送申请请求
async function sendApplyRequest(caseId:number){
    let res=await instance.put("/case/state/apply",null,{
        params:{caseId}
    })
    return ResUtil.isSuccess(res);
}
//管理员审批
async function adminExamine(agree:number,caseId:number,mediatorId:number){
    let res=await instance.put("/case/adminExamine",null,{
        params:{agree,caseId,mediatorId}
    });
    return ResUtil.isSuccess(res);
}
//助理分配案件调节时间
async function assignMediationTime(caseId:string,mediationTime:string) {
    let res=await instance.put("/case/assignMediationTime",null,{
        params:{caseId,mediationTime}
    })
    return ResUtil.isSuccess(res);
}
//调解员设置调节结果
async function setMediationResult(caseId:number,mediationResult:number){
    let res=await instance.put("/case/mediationResult",null,{
        params:{caseId,mediationResult}
    })
    return ResUtil.isSuccess(res);
}
//管理员作废案件
async function nullifyCase(caseId:number){
    let res=await instance.put("/case/nullify",null,{
        params:{caseId}
    })
    return ResUtil.isSuccess(res);
}
//获得待助理处理的案件
async function getCaseToAssistant(){
    let res=await instance.get("/case/assistant");
    return ResUtil.getData<Case[]>(res);
}

async function getCaseByRspIdAndStatus(rspId,status){
    return ResUtil.getData<Case[]>(await instance.request({
        method:'get',
        url:'/case/respondent/get',
        params:{
            rspId,status
        }
    }))
}
//把案件状态设置为已拒绝 7
async function setCaseStateToRefused(caseId){
    return ResUtil.getData(await instance.request({
        method:'put',
        url:'/case/refused',
        params:{
            caseId,
        }
    }))
}
//查询未完结案件 即状态为7/8的
async function getUnfinishedCase(respondentId){
    return ResUtil.getData<Case[]>(await instance.request({
        method:'get',
        url:'/case/unfinished',
        params:{
            respondentId,
        }
    }))
}
async function getUnfinishedCaseDetail(caseId){
    return ResUtil.getData(await instance.request({
        method:'get',
        url:'/case/unfinished/detail',
        params:{
            caseId,
        }
    }))
}
// 获取所有案件状态为4/5的案件
async function getCompletedCase(respondentId){
    return ResUtil.getData<Case[]>(await instance.request({
        method:'get',
        url:'/case/completed',
        params:{
            respondentId,
        }
    }))
}
//根据案件id查询完结案件的详细信息
async function getCompletedCaseDetail(caseId){
    return ResUtil.getData(await instance.request({
        method:'get',
        url:'/case/completed/detail',
        params:{
            caseId,
        }
    }))
}
//
async function getCaseCommentByCaseId(caseId){
    return ResUtil.getData<MediatorComment[]>(await instance.request({
        method:'get',
        url:'/case/comment/luo',
        params:{
            caseId
        }
    }))
}
//查询该案件是否有评论
async function getCaseComment(caseId,mediatorId){
    return ResUtil.getData(await instance.request({
        method:'get',
        url:'/case/comment',
        params:{
            caseId,mediatorId
        }
    }))
}
//添加新的评论
async function addCaseComment(data:MediatorComment){
    return ResUtil.isSuccess(await instance.request({
        method:'post',
        url:'/case/comment',
        data
    }))
}
//查询该案件是否下被申请人对还款案件的反馈记录
async function getCaseFeedback(caseId,rspId){
    return ResUtil.getData<PaymentHistory[]>(await instance.request({
        method:'get',
        url:'/case/feedback',
        params:{
            caseId,rspId
        }
    }))
}
//新增反馈记录
async function addCaseFeedback(data){
    return ResUtil.getData(await instance.request({
        method:'post',
        url:'/case/feedback',
        data
    }))
}
export {
    CaseStatisticLuo,
    getCaseStatisticLuo,
    getCompletedCase,
    getCompletedCaseDetail,
    getCaseComment,
    addCaseComment,
    getCaseFeedback,
    setCaseStateToRefused,
    addCaseFeedback,
    selectSelective,
    getUnfinishedCase,
    getCompletedCaseByMediatorId,
    getUnCompletedCaseByMediatorId,
    getCaseStatistic,
    getCaseByUserId,
    getCaseCommentByCaseId,
    getCaseByCaseStatus,
    addCase,
    applyCase,
    updateByApplicant,
    getAllCase,
    setRespondent,
    sendApplyRequest,
    adminExamine,
    getUnfinishedCaseDetail,
    assignMediationTime,
    setMediationResult,
    getCaseById,
    getUnCompletedCases,
    getCompletedCases,
    nullifyCase,
    getCaseToAssistant,
    getCaseByRspIdAndStatus,
    addCaseRepayment,
    getCaseRepayment
}